import { request } from '@ife/utils/http';
import { ORIGIN } from '@/constants';

// 查询所有正在使用的题目
export const queryTopicList = async (params?: any) => {
    let url = `${ORIGIN}/form/topic/list/`;
    if (params) {
        const list: string[] = [];
        Object.keys(params).forEach(key => {
            if (params[key] || key === 'topicId') {
                list.push(`${key}=${params[key]}`);
            }
        });
        const str = list.join('&');
        url = `${url}?${str}`;
    }
    const init: any = {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        },
    };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || [];
};
// 查询正在使用topic的协议列表
export const queryTopicLinkProtocols = async (topicId: number) => {
    // 发起请求
    console.log('CC_ROOT: ', ORIGIN);
    const url = `${ORIGIN}/form/protocol/${topicId}/list/`;
    const init: any = {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        },
    };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || [];
};
// 查询正在使用topic的详情
export const queryTopicLib = async (topicId: number) => {
    console.log('CC_ROOT: ', ORIGIN);
    const url = `${ORIGIN}/form/topic/${topicId}/`;
    const init: any = {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        },
    };
    const res = await request(url, init);
    const result = await res.json();
    return result?.result || [];
};
export const updateTopicLib = async (topicId: number, data?: any) => {
    const url = `${ORIGIN}/form/topic/${topicId}/`;
    const init: any = {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        body: data ? JSON.stringify(data) : '',
    };
    const res = await request(url, init);
    console.log('res: ', res);
    const result = await res.json();
    return result;
};
export const createTopic = async (data: any) => {
    const url = `${ORIGIN}/form/topic/list/`;
    const init: any = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    };
    const res = await request(url, init);
    const result = await res.json();
    return result;
};
